#include "BikeStore.h"

ostream& operator << (ostream& aOut, const BikeStore& aStore) {
	for(int i = 0; i < aStore.mSize; i++)
		aOut << aStore.mInventory[i]; 
	return aOut;
}

BikeStore::BikeStore() {
	mInventory = new Bike[10];
	mSize = 0;
	mCapacity = 10;
}

BikeStore::BikeStore(int aSize) {
	mInventory = new Bike[aSize];
	mSize = 0;
	mCapacity = aSize;
}

BikeStore::BikeStore(const Bike aBikes[], int aSize) {
	mInventory = new Bike[aSize*2];
	for(int i = 0; i < aSize; i++)
		mInventory[i] = aBikes[i];
	mSize = aSize;
	mCapacity = aSize*2;
}

BikeStore::BikeStore(const BikeStore& aBikeStore) {
	mSize = aBikeStore.mSize;
	mCapacity = aBikeStore.mCapacity;
	mInventory = new Bike[mCapacity];
	for(int i = 0; i < mSize; i++)
		mInventory[i] = aBikeStore.mInventory[i];
}

BikeStore::~BikeStore() {
	delete [] mInventory;
}

int BikeStore::getSize() const {
	return mSize;
}

int BikeStore::getCapacity() const {
	return mCapacity;
}

bool BikeStore::addBike(const Bike& aBike) {
	if(mSize == mCapacity) {
		resize();
	}
	mInventory[mSize++] = aBike;
	return true;
}

bool BikeStore::removeBike(const Bike& aBike) {
	for(int i = 0; i < mSize; i++) {
		if(mInventory[i] == aBike) {
			moveBikes(i);
			if(mCapacity >= (mSize*2)) {
				shrink();
			}
			return true;
		}
	}
	return false;
}

bool BikeStore::removeBike(int aIndex) {
	moveBikes(aIndex);
	if(mCapacity >= (mSize*2)) {
		shrink();
	}
	return true;
}

void BikeStore::moveBikes(int aIndex) {
	// shift all bikes over by one and reduce the size
	for(int i = aIndex; i < mSize-1; i++) {
		mInventory[i] = mInventory[i+1];
	}
	
	mSize--;
}

int BikeStore::resize() {
	cout << "Resize...\n";
	mCapacity *= 2;
	BikePtr tempInventory = new Bike[mCapacity];

	for(int i = 0; i < mSize; i++)
		tempInventory[i] = mInventory[i];

	delete [] mInventory;
	mInventory = tempInventory;
	return mCapacity;
}

int BikeStore::shrink() {
	cout << "Shrinking...\n";
	mCapacity = mSize;
	BikePtr tempInventory = new Bike[mCapacity];

	for(int i = 0; i < mSize; i++)
		tempInventory[i] = mInventory[i];

	delete [] mInventory;
	mInventory = tempInventory;
	return mCapacity;
}

void BikeStore::operator = (const BikeStore& aBikeStore) {
	cout << "Calling Assignment Operator\n";
	if(mCapacity < aBikeStore.mCapacity) {
		delete [] mInventory;
		mInventory = new Bike[aBikeStore.mCapacity];
		mCapacity = aBikeStore.mCapacity;
	}
	for(int i = 0; i < aBikeStore.mSize; i++) {
		mInventory[i] = aBikeStore.mInventory[i];
	}
	mSize = aBikeStore.mSize;
}
